﻿
Imports Licenta3.Common.Framework
Imports Licenta3.SetariClass
Imports Licenta3.Common.Classes
Imports Microsoft.Office.Interop.Word
Imports System.IO
Imports System.ComponentModel

<Serializable()>
Public Class Anexa1Class
    Inherits AnexaClass
    Public Sub New()
        Me.ShortName = "ANEXA1"
        Me.LongName = "Cerere de solicitare informații și convenție"

    End Sub


    Private _docCadastrala As String
    Public Property docCadastrala As String
        Get
            Return _docCadastrala
        End Get
        Set(value As String)
            'If TipDocCadastrala.values.Contains(value) Then
            _docCadastrala = value
            'End If
            OnPropertyChanged("docCadastrala")
        End Set
    End Property
    Private _informatii As String
    Public Property informatii As String
        Get
            Return _informatii
        End Get
        Set(value As String)
            _informatii = value
            OnPropertyChanged("informatii")
        End Set
    End Property
    Private _termenLucrare As String
    Public Property termenLucrare As String
        Get
            Return _termenLucrare
        End Get
        Set(value As String)
            If _termenLucrare <> value Then
                _termenLucrare = value
                OnPropertyChanged("termenLucrare")
            End If
        End Set
    End Property
    Private _startDate As Date = Today.Date
    Public Property startDate As Date
        Get
            Return _startDate
        End Get
        Set(value As Date)
            _startDate = value
            OnPropertyChanged("startDate")
        End Set
    End Property
    Private _onorariu As Decimal
    Public Property onorariu As Decimal
        Get
            Return _onorariu
        End Get
        Set(value As Decimal)
            _onorariu = value
            OnPropertyChanged("onorariu")
            _rest = _onorariu - _avans
        End Set
    End Property
    Private _onorariulReprezinta As String = "contravaloarea lucrarii"
    Public Property onorariulReprezinta As String
        Get
            Return _onorariulReprezinta
        End Get
        Set(value As String)
            _onorariulReprezinta = value
            OnPropertyChanged("onorariulReprezinta")
        End Set
    End Property
    Private _avans As Decimal
    Public Property avans As Decimal
        Get
            Return _avans
        End Get
        Set(value As Decimal)
            _avans = value
            OnPropertyChanged("avans")
            _rest = _onorariu - _avans
        End Set
    End Property
    Private _chitanta As New ChitantaClass
    Public Property chitanta As ChitantaClass
        Get
            Return _chitanta
        End Get
        Set(value As ChitantaClass)
            _chitanta = value
            OnPropertyChanged("chitanta")
        End Set
    End Property
    <NonSerialized> Public _rest As Decimal
    Private _DataPlata As Date = Today
    Public Property DataPlata As Date
        Get
            Return _DataPlata
        End Get
        Set(value As Date)
            _DataPlata = value
            OnPropertyChanged("DataPlata")
        End Set
    End Property
    Private _tipPlata As String
    Public Property tipPlata As String
        Get
            Return _tipPlata
        End Get
        Set(value As String)
            _tipPlata = value
            OnPropertyChanged("tipPlata")
        End Set
    End Property
    Private _conditiiPlata As String = "la sediul autorizatului."
    Public Property conditiiPlata As String
        Get
            Return _conditiiPlata
        End Get
        Set(value As String)
            _conditiiPlata = value
            OnPropertyChanged("conditiiPlata")
        End Set
    End Property
    Private _alteClauze As String = "nu există."
    Public Property alteClauze As String
        Get
            Return _alteClauze
        End Get
        Set(value As String)
            _alteClauze = value
            OnPropertyChanged("alteClauze")
        End Set
    End Property
    Private _NrDeOrdineIntern As Integer
    Public Property NrDeOrdineIntern As Integer
        Get
            Return _NrDeOrdineIntern
        End Get
        Set(value As Integer)
            _NrDeOrdineIntern = value
            OnPropertyChanged("NrDeOrdineIntern")
        End Set
    End Property
    Private _dataCerere As Date = Today
    Public Property dataCerere As Date
        Get
            Return _dataCerere
        End Get
        Set(value As Date)
            _dataCerere = value
            OnPropertyChanged("dataCerere")
        End Set
    End Property
    Public Enum outputType
        outWord = 1
        outPDF = 2
        outPrint = 3
    End Enum

    'Private LucruPath As String = SetariClass._LucruPath
    'Public Property SaveLocation As String
    Public Overrides Sub toWordOrPDF(Optional ByVal format As WdSaveFormat = WdSaveFormat.wdFormatDocumentDefault, Optional printOnly As Boolean = False)
        Try
            Dim i As Integer
            'Dim currentDirectory As String
            Dim Word As New Application
            Dim Anexa1Doc As New Document
            Dim sablon As Object = (assemblyPath & "\WordTemplates\ANEXA1.dotx")
            Anexa1Doc = Word.Documents.Add(sablon, False, WdNewDocumentType.wdNewBlankDocument, False)
            Dim currentDirectory = SetariClass._LucruPath & "\" & Me.Autorizat.Nume & "\" & Me.OCPI.denumire & "\" & Me.Imobil.UAT.Denumire & "\" & Me.proprietar.Nume
            If IO.Directory.Exists(currentDirectory) = False Then
                IO.Directory.CreateDirectory(currentDirectory)
            End If
            For i = Anexa1Doc.ContentControls.Count To 1 Step -1
                With Anexa1Doc.ContentControls.Item(i)
                    Select Case .Tag
                        Case "OneForPFA"
                            If Me.Autorizat.IsPF Then
                                .DropdownListEntries(1).Select()
                            Else
                                .DropdownListEntries(2).Select()
                            End If
                            'Continue For
                        Case "AutorizatIsPJ2bd"
                            If Me.Autorizat.IsPF = False Then
                                .Delete(True)
                            End If
                            'Continue For
                        Case "AvansIs02bd"
                            If Me.avans = 0 Then
                                .Delete(True)
                            End If
                            'Continue For
                        Case "NoConditii2bd"
                            If Me.conditiiPlata = "" Then
                                .Delete(True)
                            End If
                            'Continue For
                    End Select
                    Select Case .Title
                        Case "OCPI"
                            .Range.Text = Me.OCPI.denumire.ToUpper
                            'Continue For
                        Case "BCPI"
                            .Range.Text = Me.BCPI.denumire.ToUpper
                            'Continue For
                        Case "NumeAutorizat"
                            .Range.Text = Me.Autorizat.Nume
                            'Continue For
                        Case "AdresaAutorizat"
                            .Range.Text = Me.Autorizat.adresa.adresaIntreaga
                            'Continue For
                        Case "SeriaCI"
                            .Range.Text = Me.Autorizat.SerieCI
                            'Continue For
                        Case "NrCI"
                            .Range.Text = Me.Autorizat.NrCI
                            'Continue For
                        Case "ID"
                            .Range.Text = Me.Autorizat.id
                            'Continue For
                        Case "DocCadastrala"
                            .Range.Text = Me.docCadastrala
                            'Continue For
                        Case "UATImobil"
                            .Range.Text = Me.Imobil.UAT.Denumire
                        Case "AdresaImobil"
                            .Range.Text = Me.Imobil.Adresa.adresaIntreaga
                        Case "informatii"
                            .Range.Text = Me.informatii
                            'Continue For
                        Case "NumeProprietar"
                            .Range.Text = Me.proprietar.Nume
                            'Continue For
                        Case "Termen"
                            .Range.Text = Me.termenLucrare.ToString
                            'Continue For
                        Case "startDate"
                            .Range.Text = Me.startDate.Date
                            'Continue For
                        Case "Onorariu"
                            .Range.Text = Me.onorariu.ToString
                            'Continue For
                        Case "OnorariulReprezinta"
                            .Range.Text = Me.onorariulReprezinta
                            'Continue For
                        Case "avans"
                            .Range.Text = Me.avans.ToString
                            'Continue For
                        Case "NrChitanta"
                            .Range.Text = Me.chitanta.NrChitanta.ToString
                            'Continue For
                        Case "DataChitantaAvans"
                            .Range.Text = Me.chitanta.DataChitanta.Date.ToShortDateString
                            'Continue For
                        Case "Rest"
                            .Range.Text = Me._rest
                            'Continue For
                        Case "DataPlata"
                            .Range.Text = Me.DataPlata.Date
                            'Continue For
                        Case "TipPlata"
                            .Range.Text = Me.tipPlata
                            'Continue For
                        Case "ConditiiPlata"
                            .Range.Text = Me.conditiiPlata
                            'Continue For
                        Case "NumarOrdineIntern"
                            .Range.Text = Me.NrDeOrdineIntern.ToString
                            'Continue For
                        Case "alteClauze"
                            .Range.Text = Me.alteClauze
                            'Continue For
                    End Select
                End With
            Next
            'Dim format As Object = "wdFormatPDF"

            Dim ext As String = ".docx"
            If format = WdSaveFormat.wdFormatPDF Then ext = ".pdf"
            If printOnly = True Then
                Try
                    'Word.Visible = True
                    Anexa1Doc.Activate()
                    If File.Exists(currentDirectory & "\ANEXA1.docx") = False Then
                        Anexa1Doc.SaveAs2(currentDirectory & "\ANEXA1.docx")
                    Else
                        File.Delete(currentDirectory & "\ANEXA1.docx")
                        Anexa1Doc.SaveAs2(currentDirectory & "\ANEXA1.docx")
                    End If
                    Word.PrintOut(FileName:=currentDirectory & "\ANEXA1.docx")
                    'System.Threading.Thread.Sleep(1000)
                    'File.Delete(currentDirectory & "\ANEXA1.docx")
                Catch ex As Exception
                    MsgBox(ex.Message + vbCrLf + ex.Source, MsgBoxStyle.Information)
                End Try
            Else
                Try
                    If File.Exists(currentDirectory & "\ANEXA1" & ext) = False Then
                        Anexa1Doc.SaveAs2(currentDirectory & "\ANEXA1" & ext, format)
                    Else
                        If MsgBox("Fisierul exista! Doriti sa-l suprascrieti?", MsgBoxStyle.YesNo, "Sunteti sigur?") = MsgBoxResult.Yes Then
                            File.Delete(currentDirectory & "\ANEXA1" & ext)
                            Anexa1Doc.SaveAs2(currentDirectory & "\ANEXA1" & ext, format)
                        Else
                            MsgBox("Niciun fisier nu a fost modificat", MsgBoxStyle.OkOnly)
                        End If
                    End If
                Catch ex As Exception
                    MsgBox(ex.Message + vbCrLf + ex.Source + vbCrLf + ex.StackTrace, vbOKOnly)
                End Try
            End If

            Anexa1Doc.Close(WdSaveOptions.wdDoNotSaveChanges)
            Anexa1Doc = Nothing
            'Anexa1Doc.SaveAs2(currentDirectory & "\ANEXA1.docx")
            'Word.Visible = True
            Word.Quit(WdSaveOptions.wdDoNotSaveChanges)
            Word = Nothing
        Catch ex As Exception
            MsgBox(ex.Message + ex.Source + ex.StackTrace)
        End Try
    End Sub

End Class
